package ltd.newbee.mall.controller.admin;


import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.entity.NewbeeMallPurchaseInfo;
import ltd.newbee.mall.entity.NewbeeMallSupplierInfo;
import ltd.newbee.mall.service.impl.NewBeeMallGoodsServiceImpl;
import ltd.newbee.mall.service.impl.PurchaseInfoServiceImpl;
import ltd.newbee.mall.service.impl.SupplierInfoServiceImpl;
import ltd.newbee.mall.util.KeyUtil;
import ltd.newbee.mall.util.PageResult;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: NewBeeMallPurchaseController
 * @Description: 采购Controller
 * @Author: bwh
 * @Date: 2022/9/6 14:03
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class NewBeeMallPurchaseController {
    @Resource
    private SupplierInfoServiceImpl supplierInfoService;

    @Resource
    private PurchaseInfoServiceImpl purchaseInfoService;

    @Resource
    private NewBeeMallGoodsServiceImpl goodsService;

    @GetMapping("/purchase")
    public String usersPage(HttpServletRequest request) {

        List<NewbeeMallSupplierInfo> supplierInfoList = supplierInfoService.findAll();
        List<NewBeeMallGoods> goodsList = goodsService.findAll();
        request.setAttribute("path", "purchase");
        request.setAttribute("supplierInfoList", supplierInfoList);
        request.setAttribute("goodsList", goodsList);
        return "admin/newbee_mall_purchase";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/purchase/list", method = RequestMethod.GET)
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty((CharSequence) params.get("page")) || StringUtils.isEmpty((CharSequence) params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }

        Integer page = Integer.parseInt(String.valueOf(params.get("page")));
        ;
        //Integer limit = (Integer) params.get("limit");
        Integer limit = Integer.parseInt(String.valueOf(params.get("limit")));
        PageRequest pageRequest = PageRequest.of(page - 1, limit);

        PageResult pageResult = purchaseInfoService.getNewBeeMallPurchasePage(pageRequest);


        return ResultGenerator.genSuccessResult(pageResult);


    }

    /**
     * 添加
     */
    @RequestMapping(value = "/purchase/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody NewbeeMallPurchaseInfo newbeeMallPurchaseInfo) {
        if (
                 StringUtils.isEmpty(newbeeMallPurchaseInfo.getSupplierId())
                || newbeeMallPurchaseInfo.getGoodsId() == 0
                || newbeeMallPurchaseInfo.getPurchaseQuantity() == null
                || newbeeMallPurchaseInfo.getImportPrice() == null) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        newbeeMallPurchaseInfo.setPurchaseId(KeyUtil.genUniqueKey());
        newbeeMallPurchaseInfo.setGoodsName(goodsService.findGoodsNamebyId(newbeeMallPurchaseInfo.getGoodsId()));

        String result = purchaseInfoService.save( newbeeMallPurchaseInfo);

        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }
}
